<script setup>
import http from "@/util/http-util.js";
import {onMounted, ref} from "vue";
import apps from "@/config/application.js";
import {useRouter} from "vue-router";
import { Bell, Timer } from '@element-plus/icons-vue'

const router=useRouter();

const loadBanner = ()=>{
  http.GET("/consume/getBannerList").then(rep=>{
      bgList.value = rep.data;
  })
}

const loadYY = ()=>{
  http.GET("/consume/yuyueList").then(rep=>{
    reservationList.value = rep.data;
  })
}


const goout =async ()=>{
  http.logout();
  router.push("/")
}

const roomReq = ref({
  name:undefined,
  canJoin:0
})

const loadRoom = ()=>{
  http.GET("/consume/getRoomList",roomReq.value).then(rep=>{
    roomList.value = rep.data;
  })
}

const search = ()=>{
  loadRoom();
}

const goto = (url,id)=>{
  console.log(url)
  router.push(url+"?id="+id)
}


const loadNotice = ()=>{
  http.GET("/consume/getNoticeList").then(rep=>{
    noticeList.value = rep.data;
  })
}

onMounted(()=>{
  loadBanner();
  loadRoom();
  loadNotice();
})

const bgList = ref([

])
const roomList = ref([

])
const noticeList = ref([

])

const dialogVisible = ref(false)
const reservationList = ref([])

const showReservationDialog = () => {
  loadYY();
  dialogVisible.value = true
}

const handleClose = () => {
  dialogVisible.value = false
}

</script>

<template>
  <div style="width: 100vw;overflow-x: hidden">
    <div class="header">
      <el-carousel height="400px" class="banner-carousel" indicator-position="outside">
        <el-carousel-item v-for="(bg,i) in bgList" :key="i">
          <div class="banner-item">
            <el-image fit="cover" :src="http.genUrl(bg.bannerUrl)" class="banner-image"/>
            <div class="banner-content">
              <div class="banner-text">
                {{bg.introduce}}
              </div>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>

      <div class="header-nav">
        <div class="nav-left">
          <el-image :src="apps.icon" class="logo-image" />
          <span class="logo-text">{{apps.name}}</span>
        </div>

        <div class="nav-right">
          <div class="reservation-btn" @click="showReservationDialog">
            <el-icon><Guide /></el-icon>
            <span>已预约</span>
          </div>
          <el-divider direction="vertical" class="nav-divider" />
          <el-dropdown trigger="click">
            <div class="user-menu">
              <el-icon><UserFilled /></el-icon>
              <span>我的</span>
              <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
    <div style="width: calc(100vw - 100px);margin: 60px 50px;">
      <el-row >
        <el-col :span="18">
          <div class="study-room-section">
            <div class="section-header">
              <h2 class="section-title">自习室预约</h2>
              <div class="search-bar">
                <el-input
                  v-model="roomReq.name"
                  placeholder="搜索自习室"
                  prefix-icon="Search"
                  clearable
                  class="search-input"
                />
                <el-checkbox
                  v-model="roomReq.canJoin"
                  :false-value="0"
                  :true-value="1"
                  class="available-checkbox"
                >
                  只看可预约
                </el-checkbox>
                <el-button type="primary" @click="search">
                  <el-icon><Search /></el-icon>
                  搜索
                </el-button>
              </div>
            </div>

            <div class="room-grid">
              <el-card
                v-for="(room,i) in roomList"
                :key="i"
                class="room-card"
                :body-style="{ padding: '0px' }"
                shadow="hover"
              >
                <div class="room-image-wrapper">
                  <el-image
                    :src="http.genUrl(room.pic)"
                    fit="cover"
                    class="room-image"
                  />
                  <div class="room-status" :class="{ 'status-full': room.canJoinCount <= 0 }">
                    {{ room.canJoinCount > 0 ? '可预约' : '已满' }}
                  </div>
                </div>
                <div class="room-info">
                  <h3 class="room-name">{{ room.name }}</h3>
                  <div class="room-location">
                    <el-icon><LocationInformation /></el-icon>
                    <span>{{ room.local }}</span>
                  </div>
                  <div class="room-footer">
                    <span class="room-capacity" v-if="room.canJoinCount > 0">
                      剩余座位: {{ room.canJoinCount }}
                    </span>
                    <el-button
                      type="primary"
                      class="book-button"
                      :disabled="room.canJoinCount <= 0"
                      @click="goto('/room-detail',room.id)"
                    >
                      立即预约
                    </el-button>
                  </div>
                </div>
              </el-card>
            </div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="notice-container">
            <div class="notice-header">
              <el-icon><Bell /></el-icon>
              <span>通知公告</span>
            </div>
            <div class="notice-list">
              <el-scrollbar height="500px">
                <div v-for="(notice,k) in noticeList" 
                     :key="k" 
                     class="notice-item"
                     @click="goto('/notice',notice.id)">
                  <div class="notice-item-content">
                    <el-image 
                      v-if="notice.pic && notice.pic != ''" 
                      :src="http.genUrl(notice.pic)" 
                      fit="cover" 
                      class="notice-image"
                    />
                    <div class="notice-info">
                      <div class="notice-title-row" style="font-weight: bolder;font-size: 16px;">
                        {{ notice.title }}
                      </div>
                      <div class="notice-title-row">
                        {{ notice.content }}
                      </div>
                  
                    </div>
                  </div>
                  <div class="notice-meta-row" style="margin-bottom: 10px;">
                        <span class="notice-time">
                          <el-icon><Timer /></el-icon>
                          {{ notice.createTime }}
                        </span>
                        <div class="notice-stats">
                          <el-tag size="small" effect="plain">
                            <el-icon><View /></el-icon>
                            {{ notice.visit }}
                          </el-tag>
                          <el-tag size="small" effect="plain" type="success">
                            <el-icon><ChatDotRound /></el-icon>
                            {{ notice.pinglun }}
                          </el-tag>
                        </div>
                      </div>
                  <el-divider />
                </div>
              </el-scrollbar>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog
      title="已预约列表"
      v-model="dialogVisible"
      width="580px"
      :before-close="handleClose"
    >
      <div class="reservation-list" style="height: 600px; overflow-y: auto;">
        <el-empty v-if="reservationList.length === 0" description="暂无预约记录" />
        <el-card v-for="item in reservationList" 
                 :key="item.id" 
                 class="reservation-item"
                 shadow="hover">
          <div class="reservation-content">
            <div style="width: 120px;height: 90px;border-radius: 4px;overflow: hidden;">
              <el-image
              :src="http.genUrl(item.pic)"
              fit="cover"
              class="room-image"
              :preview-src-list="[http.genUrl(item.pic)]"
            />
            </div>
            <div class="reservation-info">
              <h3>{{ item.name }}</h3>
              <p><i class="el-icon-location"></i> <el-icon><LocationInformation /></el-icon>{{ item.local }}</p>
              <p>预约日期：{{ item.dateKey }}</p>
              <p>时间段：{{ item.timeSpan }}</p>
            </div>
          </div>
        </el-card>
      </div>
    </el-dialog>
  </div>

</template>

<style scoped>
.header {
  position: relative;
  width: 100vw;
  height: 400px;
  background-color: #0a0c15;
}

.banner-carousel {
  height: 100%;
}

.banner-item {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.6s ease;
}

.banner-item:hover .banner-image {
  transform: scale(1.05);
}

.banner-content {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 40px;
  background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
  color: #fff;
}

.banner-text {
  max-width: 800px;
  margin: 0 auto;
  font-size: 18px;
  line-height: 1.6;
  text-align: center;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.header-nav {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  padding: 0 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(to bottom, rgba(0,0,0,0.5), transparent);
  backdrop-filter: blur(10px);
  z-index: 10;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-image {
  width: 40px;
  height: 40px;
  border-radius: 8px;
}

.logo-text {
  font-size: 20px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.reservation-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: linear-gradient(135deg, #409EFF, #79bbff);
  color: white;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.reservation-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64,158,255,0.3);
}

.nav-divider {
  height: 20px;
  margin: 0;
  border-color: rgba(255,255,255,0.3);
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #fff;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.user-menu:hover {
  background-color: rgba(255,255,255,0.1);
}

:deep(.el-carousel__indicators) {
  transform: translateY(-20px);
}

:deep(.el-carousel__indicator--horizontal) {
  padding: 12px 4px;
}

:deep(.el-carousel__button) {
  width: 30px;
  height: 3px;
  border-radius: 3px;
  background-color: rgba(255,255,255,0.7);
  transition: all 0.3s;
}

:deep(.el-carousel__indicator.is-active .el-carousel__button) {
  background-color: #fff;
}

.notice__text{
  width: 100%;
  overflow:hidden;
  text-overflow:ellipsis;
  display:-webkit-box;
  -webkit-box-orient:vertical;
  -webkit-line-clamp:1;
}
.reservation-list {
  padding: 10px;
}

.reservation-item {
  margin-bottom: 15px;
}

.reservation-content {
  display: flex;
  gap: 20px;
}

.room-image {
  width: 120px;
  height: 90px;
  border-radius: 4px;
}

.reservation-info {
  flex: 1;
}

.reservation-info h3 {
  margin: 0 0 10px 0;
  font-size: 16px;
}

.reservation-info p {
  margin: 5px 0;
  color: #666;
  font-size: 14px;
}

.notice-container {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin: 0px 10px;
}

.notice-header {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  font-size: 18px;
  font-weight: bold;
  color: #303133;
  display: flex;
  align-items: center;
  gap: 8px;
}

.notice-list {
  padding: 10px;
}

.notice-item {
  cursor: pointer;
  transition: all 0.3s;
}

.notice-item:hover {
  background-color: #f5f7fa;
}

.notice-item-content {
  padding: 12px;
  display: flex;
  gap: 15px;
  align-items: center;
}

.notice-image {
  width: 80px;
  height: 60px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}

.notice-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.notice-title-row {
  font-size: 14px;
  color: #303133;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.notice-meta-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.notice-time {
  color: #909399;
  display: flex;
  align-items: center;
  gap: 4px;
}

.notice-stats {
  display: flex;
  gap: 8px;
}

.notice-stats .el-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 22px;
  padding: 0 8px;
}

.el-divider {
  margin: 0;
}

.study-room-section {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.section-header {
  margin-bottom: 30px;
}

.section-title {
  font-size: 28px;
  color: #303133;
  margin-bottom: 20px;
  text-align: center;
  position: relative;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #409EFF, #79bbff);
  border-radius: 3px;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
}

.search-input {
  width: 300px;
}

.available-checkbox {
  margin-right: auto;
}

.room-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  padding: 10px;
}

.room-card {
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s;
}

.room-card:hover {
  transform: translateY(-5px);
}

.room-image-wrapper {
  position: relative;
  height: 200px;
}

.room-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.room-status {
  position: absolute;
  top: 15px;
  right: 15px;
  padding: 4px 12px;
  background: #67C23A;
  color: white;
  border-radius: 20px;
  font-size: 12px;
  font-weight: bold;
}

.status-full {
  background: #F56C6C;
}

.room-info {
  padding: 15px;
}

.room-name {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
  margin: 0 0 10px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-location {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #606266;
  font-size: 14px;
  margin-bottom: 15px;
}

.room-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #EBEEF5;
}

.room-capacity {
  color: #409EFF;
  font-size: 14px;
  font-weight: 500;
}

.book-button {
  padding: 6px 16px;
}

.book-button[disabled] {
  background: #909399;
  border-color: #909399;
}
</style>